package br.com.beautycenter.view;

import br.com.beautycenter.bean.Cliente;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class SelecionarPaciente extends javax.swing.JDialog {
    
    PesquisarPaciente consultaPaciente;
    HistoricoPacotes historicoPacotes;
    
    //não é recomendado usar DefaulttableModel, mas quebra o galho
    DefaultTableModel modelo;
    
    List<Cliente> listaClientes = new ArrayList<Cliente>();
    
    public void inicilizarModal(){
        
        if(getParent() instanceof JFrame){
            historicoPacotes = (HistoricoPacotes) getParent();
            listaClientes = historicoPacotes.listaClientes;
        }else{
            consultaPaciente = (PesquisarPaciente) getParent();
            listaClientes = consultaPaciente.listaPacientes;
        }
        
        modelo = (DefaultTableModel) jTableFuncionarios.getModel();
        preencherTabela();
    }
    
    public void preencherTabela(){
        for(Cliente item: listaClientes){
            modelo.addRow(new Object[]{
                String.valueOf(item.getIdPessoa()),
                item.getNome(),
                item.getCpf()
            });
        }
    }
    
    public void retornarCliente(){
        int linha = jTableFuncionarios.getSelectedRow();
        
        if(linha > -1){
            if(historicoPacotes == null) consultaPaciente.paciente = consultaPaciente.listaPacientes.get(linha);
            else historicoPacotes.cliente = listaClientes.get(linha);
            
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "Selecione um paciente!");
        }
    }
    
    /**
     * Creates new form SelecionarCliente
     */
    public SelecionarPaciente(JDialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        inicilizarModal();
    }
    
    public SelecionarPaciente(JFrame parent, boolean modal){
        super(parent, modal);
        initComponents();
        inicilizarModal();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelPrincipal = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableFuncionarios = new javax.swing.JTable();
        jPanelBotoes = new javax.swing.JPanel();
        btnSelecionar1 = new javax.swing.JButton();
        btnCancelar1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ERP BEAUTY CENTER - Pesquisa Funcionario");

        jPanelPrincipal.setBackground(new java.awt.Color(255, 255, 255));
        jPanelPrincipal.setPreferredSize(new java.awt.Dimension(550, 200));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        jTableFuncionarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "CPF"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableFuncionarios.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableFuncionariosKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTableFuncionarios);
        if (jTableFuncionarios.getColumnModel().getColumnCount() > 0) {
            jTableFuncionarios.getColumnModel().getColumn(0).setResizable(false);
            jTableFuncionarios.getColumnModel().getColumn(0).setPreferredWidth(20);
            jTableFuncionarios.getColumnModel().getColumn(1).setResizable(false);
            jTableFuncionarios.getColumnModel().getColumn(1).setPreferredWidth(200);
            jTableFuncionarios.getColumnModel().getColumn(2).setResizable(false);
            jTableFuncionarios.getColumnModel().getColumn(2).setPreferredWidth(40);
        }

        jPanelBotoes.setBackground(new java.awt.Color(255, 255, 255));
        jPanelBotoes.setPreferredSize(new java.awt.Dimension(100, 45));

        btnSelecionar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Selecionar.png"))); // NOI18N
        btnSelecionar1.setToolTipText("SELCIONAR");
        btnSelecionar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionar1ActionPerformed(evt);
            }
        });

        btnCancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Cancelar.png"))); // NOI18N
        btnCancelar1.setToolTipText("CANCELAR");
        btnCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBotoesLayout = new javax.swing.GroupLayout(jPanelBotoes);
        jPanelBotoes.setLayout(jPanelBotoesLayout);
        jPanelBotoesLayout.setHorizontalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBotoesLayout.createSequentialGroup()
                .addContainerGap(51, Short.MAX_VALUE)
                .addComponent(btnSelecionar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanelBotoesLayout.setVerticalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoesLayout.createSequentialGroup()
                .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSelecionar1))
                .addGap(0, 4, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addGap(317, 317, 317)
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelecionar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionar1ActionPerformed
        retornarCliente();
    }//GEN-LAST:event_btnSelecionar1ActionPerformed

    private void btnCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelar1ActionPerformed
        if(historicoPacotes == null) consultaPaciente.paciente = null;
        else historicoPacotes.cliente = null;
        dispose();
    }//GEN-LAST:event_btnCancelar1ActionPerformed

    private void jTableFuncionariosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableFuncionariosKeyPressed
        //se apertar enter ao selecionar algum item da tabela
       if (evt.getKeyCode() == KeyEvent.VK_ENTER){
             btnSelecionar1.doClick();
         }
    }//GEN-LAST:event_jTableFuncionariosKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar1;
    private javax.swing.JButton btnSelecionar1;
    private javax.swing.JPanel jPanelBotoes;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableFuncionarios;
    // End of variables declaration//GEN-END:variables
}